public class Main5 {
    public static void main(String[] args) {
        //将这10个对象存入excel文件（每行一本书）中并读出到一List集合中
        //写入书对象
        Book[] books=new Book[10];
        books[0]=new Book(1,"a","z");
        books[1]=new Book(2,"b","q");
        books[2]=new Book(3,"c","s");
        books[3]=new Book(4,"d","l");
        books[4]=new Book(5,"e","z");
        books[5]=new Book(6,"f","w");
        books[6]=new Book(7,"g","z");
        books[7]=new Book(8,"h","w");
        books[8]=new Book(9,"i","y");
        books[9]=new Book(10,"j","l");
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("书单");
        XSSFRow rowhead = sheet.createRow(0);
        XSSFCell cell1 = rowhead.createCell(0);
        XSSFCell cell2 = rowhead.createCell(1);
        XSSFCell cell3 = rowhead.createCell(2);
        XSSFCell cell4 = rowhead.createCell(3);
        XSSFCell cell5 = rowhead.createCell(4);
        XSSFCell cell6 = rowhead.createCell(5);
        cell1.setCellValue("书号");
        cell2.setCellValue("书名");
        cell3.setCellValue("作者");
        cell4.setCellValue("价格");
        cell5.setCellValue("出版社");
        cell6.setCellValue("出版年份");
        XSSFRow row;
        for (int i = 0; i < books.size(); i++) {
            row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(books.get(i).getSno());
            row.createCell(1).setCellValue(books.get(i).getName());
            row.createCell(2).setCellValue(books.get(i).getAuthor());
            row.createCell(3).setCellValue(books.get(i).getPrice());
            row.createCell(4).setCellValue(books.get(i).getPublisher());
            row.createCell(5).setCellValue(books.get(i).getYear());
        }
        workbook.write(new FileOutputStream("book5.xlsx"));
        workbook.close();
        //读出数据
        List<Book> books5 = new ArrayList<>();
        XSSFWorkbook wb1 = new XSSFWorkbook("book5.xlsx");
        XSSFSheet sheet1 = wb1.getSheetAt(0);
        XSSFRow rowhead1 = sheet1.getRow(0);
        System.out.println(rowhead1.getCell(0)+ "  ");
        System.out.println(rowhead1.getCell(1)+ "  ");
        System.out.println(rowhead1.getCell(2)+ "  ");
        System.out.println(rowhead1.getCell(3)+ "  ");
        System.out.println(rowhead1.getCell(4)+ "  ");
        System.out.println(rowhead1.getCell(5)+ "  ");
        XSSFRow row1;
        for (int i = 1; i < sheet1.getLastRowNum(); i++) {
            row1=sheet1.getRow(i);
            String cno = row1.getCell(0).getStringCellValue();
            String cname = row1.getCell(1).getStringCellValue();
            String author = row1.getCell(2).getStringCellValue();
            double price = row1.getCell(3).getNumericCellValue();
            String publisher = row1.getCell(4).getStringCellValue();
            double year = row1.getCell(5).getNumericCellValue();
            Book b = new Book(cno,cname,author,price,publisher, (int) year);
            books5.add(b);
        }
        wb1.close();
    }
}
